// pages/my/orderDetail/index.js
const app = getApp()
const { baseURL } = require('../../../fetch/http')
const api = require('../../../fetch/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    id: '',
    detail: {}
  },
  async orderDetail() {
    let res = await api.orderDetail({
      order_id: this.data.id
    })
    this.setData({
      detail: res.data,
      
    })
  },
  async cancelOrder() {
    let res = await api.cancelOrder({
      order_id: this.data.id
    })
    app.toast('取消成功','success',()=>{
      wx.navigateBack()
    })
  },
  async pay() {
    let res = await api.orderPay({
      order_id: this.data.id
    })
    api.wxpay(res.data).then(res => {
      if (res.errMsg == "requestPayment:ok") {
        app.toast('支付成功','success',()=>{
          this.orderDetail()
        })
      }
    }).catch(error => {
      console.log(error);
    })
  },
  logistics() {
    wx.navigateTo({
      url: '/pages/my/logistics/index?id=' + this.data.id,
    })
  },
  async address() {
    let res = await api.address()
    let arr = res.data
    if(arr.length>0) {
      this.setData({
        address: arr[0]
      })
    }
  },
  async sign() {
    let res = await api.signFor({
      order_id: this.data.id
    })
    this.orderDetail()
  },
  goodsDetail(e) {
    const { type, id } = app.dataSet(e)
    if(type==2){
      wx.navigateTo({
        url: '/pages/home/goodsDetail/index?id=' + id,
      })
    }else {
      wx.navigateTo({
        url: '/pages/mall/goodsDetail/index?id=' + id,
      })
    }
  },
  copy(){
    wx.setClipboardData({
      data: this.data.detail.expressNo,
      success: function() {
        wx.showToast({
          title: '复制成功',
          icon:"none"
        })
      }
 });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.orderDetail()
    this.address()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})